import pika
import sys

connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='direct_logs', exchange_type='direct')#创建一个为direct类型的交换器

severity = sys.argv[1] if len(sys.argv) > 1 else 'info'#如果终端输入密钥，就为密钥，否则默认为info
message = ' '.join(sys.argv[2:]) or 'Hello World!'#终端输入的内容，没输入就为‘hello world’
channel.basic_publish(
    exchange='direct_logs', routing_key=severity, body=message)#进行绑定
print(" [x] Sent %r:%r" % (severity, message))
connection.close()